/**
 * @Member[]
*/
// 每个接口都需要接口数据模型定义
import { MemberEntity } from '../model/member'
import axios, { AxiosResponse } from 'axios'

const githubURL = "https://api.github.com"
const githubMemberURL = `${githubURL}/orgs/lemoncode/members`

export const getMembersCollections = ():Promise<MemberEntity[]> => {
  const promise = new Promise<MemberEntity[]>((resolve, reject) => {
    try {
      axios.get<MemberEntity[]>(githubMemberURL)
      .then(response => resolve(mapMemberListApiToModel(response)))
    } catch (error) {
      reject(error)
    }
  })
  return promise
}

// 数据类型转换函数，将axios.get resolve 的数据转换为 MemberEntity[] 类型
const mapMemberListApiToModel = ({data}:AxiosResponse<any[]>):MemberEntity[] => data.map(githubMember => ({
    name: githubMember.id,
    login: githubMember.login,
    avatar_url: githubMember.avatar_url
}))